package com.mode;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.Utils.MenuUtils;
import com.custom.PopImgAnimation;
import com.declare.Declare;
import com.diancan.MenuFlipPage;
import com.diancan.R;

import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class FlipItemView extends RelativeLayout {
	
	ImageView bigImg;
	TextView   nameTextView;
	TextView   priceTextView;
	TextView   desTextView;
	TextView   countTextView;
	TextView positionTextView;
	SelectedMenuObj menuObj;
	int sWidth,sHeight;
	
	public ImageView addImageView;
	public ImageView removeImageView;
	Context thisContext;

	public FlipItemView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		thisContext=context;
	}
	public FlipItemView(final Context context,SelectedMenuObj menu,int width,int height,int addResId,int removeResId,final ImageView littleImage,int position,int size) {
		super(context);
		// TODO Auto-generated constructor stub
		thisContext=context;
		menuObj=menu;
		sWidth=width;
		sHeight=height;
		this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,sHeight));	
		this.setBackgroundColor(Color.WHITE);
		
		bigImg=new ImageView(context);
		bigImg.setId(1);
		bigImg.setLayoutParams(new LayoutParams(sWidth,sHeight-80));	
		
		RelativeLayout bottomLayout=new RelativeLayout(context);
		LayoutParams lpb=new RelativeLayout.LayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,80));
		lpb.addRule(RelativeLayout.BELOW,1);
		bottomLayout.setLayoutParams(lpb);
		
		LinearLayout layout1=new LinearLayout(context);
		RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		layout1.setLayoutParams(lp);
		layout1.setOrientation(LinearLayout.VERTICAL);
		
		nameTextView=new TextView(context);
		RelativeLayout.LayoutParams lp1=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		nameTextView.setLayoutParams(lp1);
		nameTextView.setId(3);
		nameTextView.setTextColor(Color.BLACK);
		nameTextView.setTextSize(18);
		
		positionTextView=new TextView(context);
		RelativeLayout.LayoutParams lp5=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		positionTextView.setLayoutParams(lp5);
		positionTextView.setId(3);
		positionTextView.setTextColor(Color.BLACK);
		positionTextView.setTextSize(16);
		
		priceTextView=new TextView(context);
		priceTextView.setId(4);
		RelativeLayout.LayoutParams lp2=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		priceTextView.setLayoutParams(lp2);
		priceTextView.setTextColor(Color.RED);
		priceTextView.setTextSize(18);
		
		desTextView=new TextView(context);
		desTextView.setId(5);
		RelativeLayout.LayoutParams lp3=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		lp3.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		lp3.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		desTextView.setLayoutParams(lp3);
		desTextView.setTextColor(Color.BLACK);
		desTextView.setTextSize(16);
		
		LinearLayout layout2=new LinearLayout(context);
		RelativeLayout.LayoutParams lParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		lParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		lParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		layout2.setLayoutParams(lParams);
		layout2.setOrientation(LinearLayout.HORIZONTAL);
		
		LinearLayout layoutcente=new LinearLayout(context);
		RelativeLayout.LayoutParams lParamscenter=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		lParamscenter.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		lParamscenter.addRule(RelativeLayout.CENTER_IN_PARENT);
		layoutcente.setLayoutParams(lParamscenter);
		layoutcente.setOrientation(LinearLayout.VERTICAL);
		layoutcente.setGravity(Gravity.CENTER_HORIZONTAL);
		
		addImageView=new ImageView(context);
		addImageView.setLayoutParams(new LayoutParams(50,50));
		removeImageView=new ImageView(context);
		removeImageView.setLayoutParams(new LayoutParams(50,50));
		
		countTextView=new TextView(context);
		countTextView.setId(6);
		LayoutParams lp4=new LayoutParams(50,50);
		countTextView.setLayoutParams(lp4);
		countTextView.setTextColor(Color.RED);
		countTextView.setTextSize(26);	
		countTextView.setGravity(Gravity.CENTER);
		
		layout1.addView(priceTextView);
		layout1.addView(desTextView);
		layoutcente.addView(nameTextView);
		layoutcente.addView(positionTextView);
		layout2.addView(addImageView);
		layout2.addView(countTextView);
//		layout2.addView(removeImageView);
		
		bottomLayout.addView(layout1);
		bottomLayout.addView(layoutcente);
		bottomLayout.addView(layout2);
		
		this.addView(bigImg);
		this.addView(bottomLayout);
		
//		String imgpathString=context.getResources().getString(R.string.img_folder)+"//"+menuObj.getImage();
		loadImage(menuObj.getId(), bigImg);
		nameTextView.setText(menuObj.getName());
		positionTextView.setText(position+"/"+size);
		priceTextView.setText("¥"+menuObj.getPrice());
		Bitmap addbmp=BitmapFactory.decodeResource(context.getResources(),addResId);
		Bitmap removebmpBitmap=BitmapFactory.decodeResource(context.getResources(), removeResId);
		addImageView.setImageBitmap(addbmp);
		removeImageView.setImageBitmap(removebmpBitmap);
		String des=menuObj.getDescription();
		if(des!=null)
		{
			desTextView.setText(des);
		}
		if(menuObj.getCount()>0)
		{
			countTextView.setText(""+menuObj.getCount());	
			removeImageView.setVisibility(View.VISIBLE);
		}
		else {
			countTextView.setText("");			
			removeImageView.setVisibility(View.INVISIBLE);
		}
		
		//点击加号
		addImageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Declare declare=(Declare)context.getApplicationContext();
				if(declare.curDeskObj==null)
				{
					Toast toast = Toast.makeText(context, "请选择餐桌", Toast.LENGTH_SHORT); 
		            toast.show(); 
				}
				else {					
					loadlittleImage(getMenuObj().getId(),littleImage);
//					Animation animation=AnimationUtils.loadAnimation(context, R.anim.popimage_anim);
//					animation.setAnimationListener(new RemoveAnimationListener(littleImage,getMenuObj()));
					PopImgAnimation animation=new PopImgAnimation(500,sWidth,sHeight);
					animation.setAnimationListener(new RemoveAnimationListener_two(getMenuObj()));
					littleImage.startAnimation(animation);  
				}
			}
		});
		//点击减号
		removeImageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Declare  declare = (Declare) thisContext.getApplicationContext();
				List<SelectedMenuObj> menuObjs=declare.curDeskObj.getSelectedProduct().getDicMenusHashtable().get(getMenuObj().getCid());
				boolean isLock=false;
				int lockcount=0;
				Iterator<SelectedMenuObj> iterator;
				for(iterator=menuObjs.iterator();iterator.hasNext();)
				{
					SelectedMenuObj menuObj1=iterator.next();
					if (menuObj1.getId().equals(menuObj.getId())) {
						isLock=menuObj1.isCommit;
						lockcount=menuObj1.lockCount;
						break;
					}
				}
				if(isLock||menuObj.getCount()<=lockcount)
				{
					Toast toast = Toast.makeText(context, "嗨，订单已经提交了不能减了", Toast.LENGTH_SHORT); 
		            toast.show(); 
					return;
				}
				int i=getMenuObj().getCount()-1;
	            setCountValue(i);
	            //从订单中减菜
	            deleteFromOrderForm(getMenuObj());
			}
		});
	}
	public TextView getCountTextView() {
		return countTextView;
	}
	public void setCountTextView(TextView countTextView) {
		this.countTextView = countTextView;
	}
	public SelectedMenuObj getMenuObj() {
		return menuObj;
	}
	public void setMenuObj(SelectedMenuObj menuObj) {
		this.menuObj = menuObj;
	}
	private void loadImage(final String url, final ImageView img) {
		//加载本地图片
		BitmapFactory.Options options=new BitmapFactory.Options();
		options.inSampleSize=1;
		Bitmap bmpDefaultPic = MenuUtils.GetRepiceBitMap(url);
		bmpDefaultPic=Bitmap.createScaledBitmap(bmpDefaultPic, sWidth, sHeight-80, false);
		img.setImageBitmap(bmpDefaultPic);
	}
	public void setCountValue(int count) {
		if(count>0)
		{
			countTextView.setText(""+count);
			removeImageView.setVisibility(View.VISIBLE);
			menuObj.setCount(count);
		}
		else {
			menuObj.setCount(0);
			countTextView.setText("");
			removeImageView.setVisibility(View.INVISIBLE);
		}	
		menuObj.setTotalPrice(menuObj.getCount()*menuObj.getPrice());
	}
	private void loadlittleImage(String url, ImageView img) {
		//加载本地图片
//		String imgpathString=getResources().getString(R.string.img_folder)+"//"+url;
		BitmapFactory.Options options=new BitmapFactory.Options();
        options.inSampleSize = 10;//数值越大图像质量越差
		Bitmap bmpDefaultPic = MenuUtils.GetRepiceBitMap(url);
		bmpDefaultPic=Bitmap.createScaledBitmap(bmpDefaultPic, 10, 10, false);
		img.setImageBitmap(bmpDefaultPic);
	}
	//加入订单
	public void AddToOrderForm(SelectedMenuObj menu)
	{
		Declare  declare = (Declare) thisContext.getApplicationContext();
        declare.curDeskObj.getSelectedProduct().AddMenu(menu);
        declare.curDeskObj.getSelectedProduct().setbState(false);
	}
	//从订单中减菜
	public void deleteFromOrderForm(SelectedMenuObj menu)
	{
		Declare  declare = (Declare) thisContext.getApplicationContext();
		if(menu.getCount()<=0)
        {
       	 declare.curDeskObj.getSelectedProduct().DeleteMenu(menu);	
        }
        else {
       	 declare.curDeskObj.getSelectedProduct().RemoveMenu(menu);
		}	
	}
	
	private class RemoveAnimationListener implements AnimationListener{
		private ImageView littleImg;
		private SelectedMenuObj menuInfo;
		public RemoveAnimationListener(ImageView img,SelectedMenuObj menu)
		{
			littleImg=img;
			menuInfo=menu;
		}
		//该方法在淡出效果执行结束之后被调用 
		 @Override
		 public void onAnimationEnd(Animation animation) {
			 Animation animation2=AnimationUtils.loadAnimation(thisContext, R.anim.descend_anim);
			 animation2.setAnimationListener(new RemoveAnimationListener_two(menuInfo));
			 littleImg.startAnimation(animation2);  
		 }  
		 //该方法在动画重复执行的时候调用
		 @Override
		 public void onAnimationRepeat(Animation animation) {
//			 System.out.println("repeat");
		 }  
		 //该方法在动画开始执行的时候，调用 
		 @Override
		 public void onAnimationStart(Animation animation) {
//			 System.out.println("start");
		 }  
		        
	 }  
	private class RemoveAnimationListener_two implements AnimationListener{
		private SelectedMenuObj menuInfo;
		public RemoveAnimationListener_two(SelectedMenuObj menu)
		{
			menuInfo=menu;
		}
		 //该方法在淡出效果执行结束之后被调用 
		 @Override
		 public void onAnimationEnd(Animation animation) {
			Intent in = new Intent();
            in.setAction("animation");
            in.addCategory(Intent.CATEGORY_DEFAULT);
            thisContext.sendBroadcast(in);
            int i=getMenuObj().getCount()+1;
            setCountValue(i);
            //加入订单
            AddToOrderForm(getMenuObj());
		 }  
		 //该方法在动画重复执行的时候调用
		 @Override
		 public void onAnimationRepeat(Animation animation) {
//			 System.out.println("repeat");
		 }  
		 //该方法在动画开始执行的时候，调用 
		 @Override
		 public void onAnimationStart(Animation animation) {
//			 System.out.println("start");
		 }  
		        
	 }  
}
